package com.chocho.producerserver.service;

import com.alibaba.fastjson.JSON;
import com.chocho.producerserver.bean.Paper;
import com.chocho.producerserver.bean.Question;
import com.chocho.producerserver.bean.Test;
import com.chocho.producerserver.client.ConsumeClient;
import com.chocho.producerserver.utils.CacheMapUtil;
import com.chocho.producerserver.utils.JwtUtil;
import com.chocho.producerserver.utils.MQSend;
import com.chocho.producerserver.vo.Code;
import com.chocho.producerserver.vo.TokenInfo;
import com.chocho.producerserver.vo.VPaper;
import com.chocho.producerserver.vo.VQuestion;
import io.jsonwebtoken.Claims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.tools.*;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chocho on 2022/2/26.
 */
@Service
public class getWayService {
    @Autowired
    private ConsumeClient consumeClient;
    @Autowired
    private PaperService paperService;
    @Autowired
    private TestService testService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private MQSend mqSend;

    private static final String REPLACE_STRING = "CHOCHO_JOPS_REPLACE_SEGMENT";

    private Logger logger = LoggerFactory.getLogger(getWayService.class);

    public void compileJava(HttpServletRequest request, Code code){
        String token = request.getHeader("token");
        try {
            Claims claims = JwtUtil.parseJWT(token);
            code.setUserId(claims.getSubject());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }
        try {
            String jsonString = JSON.toJSONString(code);
            mqSend.sendMessage(jsonString);
            logger.debug("MQ send json message: "+jsonString);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("MQ sending message failed!");
            throw new RuntimeException("发送消息失败!请重试!");
        }
    }

    public Map<String, Object> checkTest(List<String> testIds){
        //从数据库获取test输入输出
        List<Test> testList = testService.getByIdList(testIds);

        if(testList != null && testList.size() > 0){
            //从数据库获取题目标准答案
            String questionId = testList.get(0).getQuestionId();
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("questionId", questionId);
            List<VQuestion> question = questionService.getQuestionByQuery(map);

            //TODO 拼接输入输出、标准答案生成测试程序
            String input = "";
            for(Test t : testList){
                input = input + "";
            }
            VQuestion vq = question.get(0);
            String joinAnswer = vq.getAnswer1() + vq.getAnswer2() + vq.getAnswer3();
            String stardandAnswer = joinAnswer.replaceAll(input, REPLACE_STRING);
            //TODO 通过http与消费者服务进行连接，将测试程序发送给编译服务
            //TODO 将结果反馈给用户，若全部成功则返回200，若失败则返回当前的样例错误信息
        }
        return null;
    }

    public void compileJavaNOSQL(Code code){
        code.setQuestionId("000");
        try {
            String jsonString = JSON.toJSONString(code);
            mqSend.sendMessage(jsonString);
            logger.debug("MQ send json message: "+jsonString);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("MQ sending message failed!");
            throw new RuntimeException("发送消息失败!请重试!");
        }
    }
}